package com.tietiao.common.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.imageio.ImageIO;

public class ImageUtils {
	/**
	 * 缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入流
	 * @param output
	 *            输出流
	 * @param format
	 *            输出格式
	 * @return
	 * @throws Exception
	 */
	public static boolean convert(int width, int height, InputStream input,OutputStream output, String format) throws Exception {
		// 输入
		BufferedImage inputImage = ImageIO.read(input);
		// 转换
		RenderedImage im = (RenderedImage) convert(height, height, inputImage);
		// 输出
		return ImageIO.write(im, format, output);
	}

	/**
	 * 转换压缩算法
	 * 
	 * @param input
	 *            输入文件
	 * @param output
	 *            输出文件
	 * @return
	 * @throws Exception
	 */
	public static boolean convert(File input, File output) throws Exception {
		// 输入
		BufferedImage inputImage = ImageIO.read(input);

		// 转换
		int width = inputImage.getWidth();
		int height = inputImage.getHeight();

		RenderedImage im = (RenderedImage) convert(width, height, inputImage);
		String outputFilename = output.getName();
		String format = outputFilename.substring(outputFilename.lastIndexOf('.') + 1);
		// 输出
		return ImageIO.write(im, format, output);
	}

	/**
	 * 缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入文件
	 * @param output
	 *            输出文件
	 * @return
	 * @throws Exception
	 */
	public static boolean convert(int width, int height, File input, File output)throws Exception {
		// 输入
		BufferedImage inputImage = ImageIO.read(input);
		// 转换
		RenderedImage im = (RenderedImage) convert(width, height, inputImage);
		String outputFilename = output.getName();
		String format = outputFilename.substring(outputFilename.lastIndexOf('.') + 1);
		// 输出
		return ImageIO.write(im, format, output);
	}

	/**
	 * 缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入路径
	 * @param output
	 *            输出路径
	 * @return
	 * @throws Exception
	 */
	public static boolean convert(int width, int height, String inputPath,String outputPath) throws Exception {
		return convert(width, height, new File(inputPath), new File(outputPath));
	}

	/**
	 * 转换
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            BufferedImage
	 * @return BufferedImage
	 * @throws Exception
	 */
	private static BufferedImage convert(int width, int height,BufferedImage input) throws Exception {
		// 初始化输出图片
		BufferedImage output = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
		// 重新绘图
		Image image = input.getScaledInstance(output.getWidth(), output.getHeight(), output.getType());
		output.createGraphics().drawImage(image, null, null);
		return output;
	}

	/**
	 * 等比缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入流
	 * @param output
	 *            输出流
	 * @return
	 * @throws Exception
	 */
	public static boolean equimultipleConvert(int width, int height,String input, String output) throws Exception {
		return equimultipleConvert(width, height, new File(input), new File(output));
	}

	/**
	 * 等比缩放图片
	 * 
	 * @param width
	 *            输出宽度
	 * @param height
	 *            输出高度
	 * @param input
	 *            输入流
	 * @param output
	 *            输出流
	 * @return
	 * 
	 * @throws Exception
	 */
	public static boolean equimultipleConvert(int width, int height,File input, File output) throws Exception {
		// 输入
		BufferedImage image = ImageIO.read(input);
		// 重新核算尺寸
		if (image.getWidth() > 0 && image.getHeight() > 0) {
			if ((image.getWidth() / image.getHeight()) >= (width / height)) {
				if (image.getWidth() > width) {
					height = (image.getHeight() * width) / image.getWidth();
				} else {
					width = image.getWidth();
					height = image.getHeight();
				}
			} else {
				if (image.getHeight() > height) {
					width = (image.getWidth() * height) / image.getHeight();
				} else {
					width = image.getWidth();
					height = image.getHeight();
				}
			}
		}

		// 转换 输出
		return convert(width, height, input, output);
	}
	
	// 处理图片
	public static BufferedImage doPic(File file) throws Exception{
		BufferedImage bufferedImage = ImageIO.read(file);
		int w = bufferedImage.getWidth();
		int h = bufferedImage.getHeight();
		int max=0,min=0,x=0;
		if(w>h){
			max = w;
			min = h;
		}else if(w<h){
			max = h;
			min = w;
		}else{
			max = h;
			min = w;
		}
		x = max-min;
		BufferedImage tag = new BufferedImage(max,max,BufferedImage.TYPE_INT_RGB);
		Graphics gr = tag.getGraphics();
		gr.setColor(Color.BLACK);
		if(w>h){
			gr.fillRect(0, 0, w, x/2);
			gr.drawImage(bufferedImage,0,x/2,w,h,null);
			gr.fillRect(0, x/2+h, w, x/2);
		}else if(w<h){
			gr.fillRect(0, 0, x/2, h);
			gr.drawImage(bufferedImage,x/2,0,w,h,null);
			gr.fillRect( x/2+w,0, x/2, h);
		}else{
			tag = bufferedImage;
		}			
		return tag;
	}
	
	/**
	 * 转换灰度图
	 * @param img
	 * @return
	 */
	public static BufferedImage convGrayImage(BufferedImage img) {
        if(img.getType()==BufferedImage.TYPE_BYTE_GRAY) return img;
        BufferedImage gimg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = gimg.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();
        return gimg;
    }
	/**
	 * 
	 * 功能：从网上下载图片到本地
	 * @author: whh 
	 * @Date: 2012-1-14
	 * @return
	 */
	public static boolean convertFromNet(int width, int height,URL url,String outputFile){
	 	String format = outputFile.substring(outputFile.lastIndexOf('.') + 1);
        try {
        	 java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());  
			convert(100, 200, bis, new FileOutputStream(new File(outputFile)),format);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
